<template>
  <div class="return-manage">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>退货管理</span>
          <el-button type="primary" @click="handleExport">导出记录</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="订单号">
          <el-input v-model="searchForm.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="退货状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="待审核" value="pending" />
            <el-option label="已同意" value="approved" />
            <el-option label="已拒绝" value="rejected" />
            <el-option label="退款中" value="refunding" />
            <el-option label="已完成" value="completed" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 退货列表 -->
      <el-table :data="returnList" border style="width: 100%">
        <el-table-column prop="returnNo" label="退货单号" width="180" />
        <el-table-column prop="orderNo" label="订单号" width="180" />
        <el-table-column label="商品信息" min-width="300">
          <template #default="scope">
            <div class="product-info">
              <el-image
                :src="scope.row.productImage"
                style="width: 50px; height: 50px"
                :preview-src-list="[scope.row.productImage]"
              />
              <div class="info-content">
                <div class="product-name">{{ scope.row.productName }}</div>
                <div class="product-spec">{{ scope.row.productSpec }}</div>
              </div>
              <div class="return-quantity">
                退货数量：{{ scope.row.returnQuantity }}
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="returnAmount" label="退款金额" width="120">
          <template #default="scope">
            ¥{{ scope.row.returnAmount.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="returnReason" label="退货原因" width="150" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button 
              v-if="scope.row.status === '待审核'"
              type="primary" 
              size="small" 
              @click="handleApprove(scope.row)"
            >
              审核
            </el-button>
            <el-button 
              v-if="['已同意', '退款中'].includes(scope.row.status)"
              type="success" 
              size="small" 
              @click="handleRefund(scope.row)"
            >
              退款
            </el-button>
            <el-button 
              type="info" 
              size="small" 
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 审核对话框 -->
    <el-dialog
      title="退货审核"
      v-model="approveDialogVisible"
      width="500px"
    >
      <el-form :model="approveForm" :rules="approveRules" ref="approveFormRef" label-width="100px">
        <el-form-item label="审核结果" prop="result">
          <el-radio-group v-model="approveForm.result">
            <el-radio label="approve">同意</el-radio>
            <el-radio label="reject">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理备注" prop="remark">
          <el-input 
            type="textarea" 
            v-model="approveForm.remark" 
            rows="3"
            placeholder="请输入处理备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="approveDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitApprove">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 退款对话框 -->
    <el-dialog
      title="退款处理"
      v-model="refundDialogVisible"
      width="500px"
    >
      <el-form :model="refundForm" :rules="refundRules" ref="refundFormRef" label-width="100px">
        <el-form-item label="退款金额" prop="amount">
          <el-input-number 
            v-model="refundForm.amount" 
            :precision="2" 
            :step="0.01" 
            :min="0"
          />
        </el-form-item>
        <el-form-item label="退款方式" prop="method">
          <el-select v-model="refundForm.method" placeholder="请选择退款方式">
            <el-option label="原路退回" value="original" />
            <el-option label="退到余额" value="balance" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理备注">
          <el-input 
            type="textarea" 
            v-model="refundForm.remark" 
            rows="3"
            placeholder="请输入处理备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="refundDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitRefund">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog
      title="退货详情"
      v-model="detailDialogVisible"
      width="700px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="退货单号">{{ currentReturn.returnNo }}</el-descriptions-item>
        <el-descriptions-item label="订单号">{{ currentReturn.orderNo }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ currentReturn.createTime }}</el-descriptions-item>
        <el-descriptions-item label="退货状态">
          <el-tag :type="getStatusType(currentReturn.status)">
            {{ currentReturn.status }}
          </el-tag>
        </el-descriptions-item>
      </el-descriptions>

      <el-divider content-position="left">商品信息</el-divider>
      <div class="product-detail">
        <el-image
          :src="currentReturn.productImage"
          style="width: 80px; height: 80px"
          :preview-src-list="[currentReturn.productImage]"
        />
        <div class="detail-content">
          <div class="product-name">{{ currentReturn.productName }}</div>
          <div class="product-spec">{{ currentReturn.productSpec }}</div>
          <div class="return-info">
            <span>退货数量：{{ currentReturn.returnQuantity }}</span>
            <span>退款金额：¥{{ currentReturn.returnAmount?.toFixed(2) }}</span>
          </div>
        </div>
      </div>

      <el-divider content-position="left">退货原因</el-divider>
      <div class="reason-content">
        <p>{{ currentReturn.returnReason }}</p>
        <div class="image-list" v-if="currentReturn.images?.length">
          <el-image
            v-for="(img, index) in currentReturn.images"
            :key="index"
            :src="img"
            style="width: 100px; height: 100px"
            :preview-src-list="currentReturn.images"
          />
        </div>
      </div>

      <el-divider content-position="left">处理记录</el-divider>
      <el-timeline>
        <el-timeline-item
          v-for="(record, index) in currentReturn.records"
          :key="index"
          :timestamp="record.time"
          :type="record.type"
        >
          {{ record.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  orderNo: '',
  status: '',
  dateRange: []
})

// 退货列表数据
const returnList = ref([
  {
    returnNo: 'TH20240318001',
    orderNo: 'DD20240318001',
    productName: '高级猫粮 - 三文鱼味',
    productSpec: '2kg装',
    productImage: 'https://placeholder.com/100',
    returnQuantity: 1,
    returnAmount: 199.00,
    returnReason: '商品包装破损',
    status: '待审核',
    createTime: '2024-03-18 10:00:00',
    images: [
      'https://placeholder.com/100',
      'https://placeholder.com/100'
    ],
    records: [
      {
        time: '2024-03-18 10:00:00',
        type: 'primary',
        content: '用户提交退货申请'
      }
    ]
  },
  {
    returnNo: 'TH20240318002',
    orderNo: 'DD20240318002',
    productName: '狗狗磨牙玩具',
    productSpec: '大号',
    productImage: 'https://placeholder.com/100',
    returnQuantity: 2,
    returnAmount: 59.80,
    returnReason: '商品质量问题',
    status: '已同意',
    createTime: '2024-03-18 09:30:00',
    images: [
      'https://placeholder.com/100'
    ],
    records: [
      {
        time: '2024-03-18 09:45:00',
        type: 'success',
        content: '客服同意退货申请'
      },
      {
        time: '2024-03-18 09:30:00',
        type: 'primary',
        content: '用户提交退货申请'
      }
    ]
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 审核对话框
const approveDialogVisible = ref(false)
const approveFormRef = ref(null)
const approveForm = reactive({
  result: '',
  remark: ''
})

const approveRules = {
  result: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
  remark: [{ required: true, message: '请输入处理备注', trigger: 'blur' }]
}

// 退款对话框
const refundDialogVisible = ref(false)
const refundFormRef = ref(null)
const refundForm = reactive({
  amount: 0,
  method: '',
  remark: ''
})

const refundRules = {
  amount: [{ required: true, message: '请输入退款金额', trigger: 'blur' }],
  method: [{ required: true, message: '请选择退款方式', trigger: 'change' }]
}

// 详情对话框
const detailDialogVisible = ref(false)
const currentReturn = reactive({})

// 方法定义
const handleSearch = () => {
  ElMessage.success('搜索功能待实现')
}

const resetSearch = () => {
  searchForm.orderNo = ''
  searchForm.status = ''
  searchForm.dateRange = []
}

const getStatusType = (status) => {
  const types = {
    '待审核': 'warning',
    '已同意': 'primary',
    '已拒绝': 'danger',
    '退款中': 'warning',
    '已完成': 'success'
  }
  return types[status] || 'info'
}

const handleApprove = (row) => {
  approveForm.result = ''
  approveForm.remark = ''
  approveDialogVisible.value = true
}

const handleRefund = (row) => {
  refundForm.amount = row.returnAmount
  refundForm.method = ''
  refundForm.remark = ''
  refundDialogVisible.value = true
}

const handleDetail = (row) => {
  Object.assign(currentReturn, row)
  detailDialogVisible.value = true
}

const handleExport = () => {
  ElMessage.success('导出功能待实现')
}

const submitApprove = async () => {
  if (!approveFormRef.value) return
  
  await approveFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('审核处理成功')
      approveDialogVisible.value = false
    }
  })
}

const submitRefund = async () => {
  if (!refundFormRef.value) return
  
  await refundFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('退款处理成功')
      refundDialogVisible.value = false
    }
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.product-info {
  display: flex;
  align-items: center;
}

.info-content {
  flex: 1;
  margin: 0 15px;
}

.product-name {
  font-size: 14px;
  color: #333;
}

.product-spec {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.return-quantity {
  font-size: 14px;
  color: #666;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.product-detail {
  display: flex;
  margin: 20px 0;
}

.detail-content {
  margin-left: 20px;
}

.return-info {
  margin-top: 10px;
  color: #666;
  display: flex;
  gap: 20px;
}

.reason-content {
  padding: 10px;
  background-color: #f8f8f8;
  border-radius: 4px;
}

.image-list {
  margin-top: 10px;
  display: flex;
  gap: 10px;
}
</style> 